<?php
namespace App\Api;
use PhalApi\Api;

/**
 * 默认接口服务类
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Site extends Api {
    public function getRules() {
        return array(
            'index' => array(
                'username'  => array('name' => 'username', 'default' => 'PhalApi', 'desc' => '用户名'),
            ),
            'ip' => array(
                'name'  => array('name' => 'name', 'default' => 'JD-CPC', 'desc' => '用户名'),
            ),
        );
    }
    /**
     * 默认ip排重接口
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
     * @return string title 标题
     * @return string content 内容
     * @return string version 版本，格式：X.X.X
     * @return int time 当前时间戳
     * @exception 400 非法请求，参数传递错误
     */
    public function ip() {
        $ip = $_SERVER['REMOTE_ADDR'];
        $get_ip_info_url = "http://api.okayapi.com/?s=Ext.IP.GetInfo&return_data=0&ip=" . $ip . "&app_key=90BB9337D7B1ACC0CA642C5726D227CB";
        // 小白接口的读ip接口 url
        $curl = new \PhalApi\CUrl();
        // 第二个参数，表示超时时间，单位为毫秒
        $rs = $curl->get($get_ip_info_url, 3000);
        if ($rs) {
            $rs = json_decode($rs, true);
            $disable = array(
                "上海",
                "上海市",
                "辽宁",
                "辽宁省",
            );


            if ( in_array($rs['data']['data']['region'], $disable) ){
                $res['state'] = false;
                $res['msg'] = "disable region";
                $res['area'] = $rs['data']['data']['region'];
                return $res;
            }


            $url = 'http://wenfree.cn/api/Public/idfa/?service=Ip.Checkip&name='.$this->name.'&ip=' . $ip;
            $r = $curl->get($url, 3000);
            $r = json_decode($r, true);


            if ( $r['data']['state'] == "ok" ){
                $r['data']['area'] = $rs['data']['data']['region'];
                return $r['data'];
            }

            return $r['data'];

        }


    }

    /**
     * 默认接口服务
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
     * @return string title 标题
     * @return string content 内容
     * @return string version 版本，格式：X.X.X
     * @return int time 当前时间戳
     * @exception 400 非法请求，参数传递错误
     */
    public function index() {
        return array(
            'title' => 'Hello ' . $this->username,
            'version' => PHALAPI_VERSION,
            'time' => $_SERVER['REQUEST_TIME'],
            'ip' => $_SERVER['REMOTE_ADDR'],
        );
    }
}
